package com.fizzkai.p2p.mgrsite.controller;

import com.fizzkai.p2p.core.domain.SystemDictionary;
import com.fizzkai.p2p.core.domain.SystemDictionaryItem;
import com.fizzkai.p2p.core.query.SystemDictionaryItemQuery;
import com.fizzkai.p2p.core.query.SystemDictionaryQuery;
import com.fizzkai.p2p.core.service.ISystemDictionaryItemService;
import com.fizzkai.p2p.core.service.ISystemDictionaryService;
import com.fizzkai.p2p.core.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService dictionaryService;
    @Autowired
    private ISystemDictionaryItemService dictionaryItemService;

    @RequestMapping("/systemDictionary_list")
    public String systemDictionary_list(@ModelAttribute("qo") SystemDictionaryQuery qo, Model model){
        PageInfo<SystemDictionary> pageInfo =  dictionaryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "systemdic/systemDictionary_list";
    }

    /**
     * 数据字典 跟数据 更新
     * @return
     */
    @RequestMapping("/systemDictionary_update")
    @ResponseBody
    public Object systemDictionary_update(SystemDictionary entity){
        dictionaryService.saveOrUpdate(entity);
        return new AjaxResult("保存成功");
    }
    
    @RequestMapping("/systemDictionaryItem_list")
    public String systemDictionaryItem_list(@ModelAttribute("qo") SystemDictionaryItemQuery qo, Model model){
        PageInfo<SystemDictionaryItem> pageInfo = dictionaryItemService.queryList(qo);
        List<SystemDictionary> systemDictionaryGroups = dictionaryService.list();
        model.addAttribute("systemDictionaryGroups", systemDictionaryGroups);
        model.addAttribute("pageInfo", pageInfo);
        return "systemdic/systemDictionaryItem_list";
    }

    /**
     * 数据字典 子数据 更新
     *
     * @return
     */
    @RequestMapping("/systemDictionaryItem_update")
    @ResponseBody
    public Object systemDictionaryItem_update(SystemDictionaryItem entity) {
        if (entity.getParentId() == null) {
            return new AjaxResult("请选择 数据字典分类", false);
        }
        int row = dictionaryItemService.saveOrUpdate(entity);
        return new AjaxResult("保存成功");
    }
}
